 
// pages/devices/devices.ts
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    c:{
      items:[],
      index:app.globalData.defaultCustomIndex
    },
    g:{
      items:[],
      index:0
    }, 
    bs:[],
    devs:{},  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var me = this;
    try {
      var c = me.data.c;
      c.index = app.globalData.defaultCustomIndex
      me.setData({
        c:c
      })
    } catch (error) {
      console.log(error);
    }
  },
  refreshCustom(){
    var me = this; 
    var resData ={
      items:[],
      index: me.data.c.index
    }; 
    return new Promise((success, faild)=>{
      try {    
        app.seed.callApi('device/getCustoms',{ }).then(res=>{ 
          resData.items = res;
          var flag = res && res.length>0 && resData.index<res.length;
          resData.index =flag?resData.index :0;
          me.setData({c:resData},()=>{success();});
        })
      } catch (error) {
        console.log(error);
        success();
      }
    });
  },
  refreshGrain(){
    var me = this; 
    var resData ={
      items:[],
      index: me.data.c.items.length>0?me.data.g.index:0
    };
    return new Promise((success, faild)=>{
      try {    
        if( me.data.c.items.length>0){
          var customId = me.data.c.items[me.data.c.index].customId;
          app.seed.callApi('device/getGrains',{ customId }).then(res=>{ 
            resData.items = res;
            var flag = res && res.length>0 && resData.index<res.length;
            resData.index =flag?resData.index :0;
            me.setData({g:resData},()=>{success();});
          }) 
        }
        else{
          me.setData({g:resData},()=>{success();});
        }
      } catch (error) {
        console.log(error);
        success();
      }
    });
  }, 
  refreshBarnDevice(){
    var me = this; 
    var resData ={
      bs:[],
      devs:{}
    };
    return new Promise((success, faild)=>{
      try {    
        if( me.data.g.items.length>0){
          var grainId = me.data.g.items[me.data.g.index].grainId;
          var devReals = {};
          app.seed.getDeviceDatas('g' , grainId).then(devDatas=>{
            devReals = devDatas;
            return app.seed.callApi('device/getBarnDevices',{ grainId })
          }).then(res=>{ 
            resData.bs = res[1];
            resData.bs.forEach(b=>{
              b.devices = res[0].filter(d=>{
                return d.barnId == b.barnId;
              });
            })
            res[0].forEach(d=>{
              resData.devs[d.snCode] = d;
              d.realData = devReals[d.snCode]||{};
            })
            me.setData(resData,()=>{success();}) ;
          }) 
        }
        else{
          me.setData(resData,()=>{success();}) ;
        }
      } catch (error) {
        console.log(error);
        success();
      }
    });
  },
  setCustom(en){
    var me = this;
    try {
      var index = en.detail.value;
      app.globalData.defaultCustomIndex = index;
      var c = me.data.c;
      c.index = index;
      me.setData({c:c},()=>{
        me.refreshGrain().then(_=>{
          return me.refreshBarnDevice();
        }).then(_=>{
          console.log('refresh custom...')
        });
      })
    } catch (error) {
      console.log(error);
    }
  },
  setGrain(en){
    var me = this;
    try {
      var index = en.currentTarget.dataset.index;
      var g = me.data.g;
      g.index = index;
      me.setData({g},()=>{
        me.refreshBarnDevice().then(_=>{
          console.log('refresh grain...', me.data.bs , me.data.devs)
        });
      })
    } catch (error) {
      console.log(error);
    }
  }, 
  onPickDevice(en){
    var me = this;
    try {
      var snCode = en.currentTarget.dataset.sn;  
      wx.navigateTo({
        url:'./real?sn='+snCode
      })
    } catch (error) {
      console.log(error)
    }
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var me = this;
    var c = me.data.c;
    c.index = app.globalData.defaultCustomIndex
    me.setData({
      c:c
    })
    me.refreshCustom().then(_=>{
      return me.refreshGrain();
    }).then(_=>{
      me.refreshBarnDevice();
    }).then(_=>{
      console.log(me.data)
    })
    //this.resetCustom();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})